package net.getbang.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import net.getbang.api.annotation.ValidateLogin;
import net.getbang.api.utils.JwtUtils;
import net.getbang.common.utils.Result;
import net.getbang.system.domain.SysUser;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/api")
public class TestController {

    @Autowired
    private JwtUtils jwtUtils;

  
    @GetMapping("/login")
    public Result login(String username, String password){
        //用户信息
       // SysUser user = sysUserService.queryByUserName(username);
    	 SysUser user =new SysUser();
    	 user.setUsername("admin");
    	 user.setPassword("admin");user.setId(1l);
        //账号不存在
        if(user == null) {
            return Result.error("账号不存在");
        }

        //密码错误
        /*if(!user.getPassword().equals(new Sha256Hash(password, user.getSalt()).toHex())) {
            return Result.error("密码不正确");
        }*/

        //生成token
        String token = jwtUtils.generateToken(user.getId());

        Map<String, Object> map = new HashMap<>();
        map.put("userId", user.getId());
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        Result r=Result.ok().put(map);
        return r;
    }

    @ValidateLogin
    @GetMapping("/test")
    public Result test(String token){
    	System.out.println(token);
    	System.out.println(jwtUtils.getUserIdFromToken(token));
        return Result.ok();
    }

}
